package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.core.common.log.LoggerUtils;
import com.jule.teenpatti.base.enums.ErrorCodeEnum;
import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_Choose_PlayTypeAck_50017;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.TableService;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 换牌
 */
public class JoloGame_Choose_PlayTypeReq_50017 extends ClientReq {
    private long time;
    private final static Logger logger = LoggerFactory.getLogger(JoloGame_Choose_PlayTypeReq_50017.class);

    public JoloGame_Choose_PlayTypeReq_50017(int functionId) {
        super(functionId);
    }

    private JoloGame.JoloGame_Choose_PlayTypeReq req;


    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_Choose_PlayTypeReq.parseFrom(blob);
        this.setTable(TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId()));
    }

    @Override
    public void processImpl() throws Exception {
        logger.debug("收到消息-> " + functionId + " reqNum-> " + header.reqNum + "msg ->" + req.toString());
        JoloGame.JoloGame_Choose_PlayTypeAck.Builder ack = JoloGame.JoloGame_Choose_PlayTypeAck.newBuilder();
        String userId = req.getUserId();
        String roomId = req.getRoomId();
        String tableId = req.getTableId();
        String gameOrderId = req.getGameOrderId();
        String dealerPlayType = req.getPlayType();

        try {
            AbstractTable table = getTable();
            ack.setUserId(userId);
            ack.setRoomId(roomId);
            ack.setTableId(tableId);
            ack.setGameOrderId(gameOrderId);

            //如果gameOrderId不一致，那么返回错误
            if (!gameOrderId.equals(table.getCurrGameOrderId())) {
                ack.setResult(-1);
                logger.error("GameOrderId不一致, req gameOrderId->" + gameOrderId);
                ack.setResultMsg(ErrorCodeEnum.GAME_50013_2.getCode());
                ctx.writeAndFlush(new JoloGame_Choose_PlayTypeAck_50017(ack.build(), header));
                return;
            }

            table.setDealerPlayType(Integer.parseInt(dealerPlayType));
            //广播玩家选定的玩法
            table.choosePlayType(false);
//        NoticeBroadcastMessages.selectedPlayType(table);
            ctx.writeAndFlush(new JoloGame_Choose_PlayTypeAck_50017(ack.setResult(1).build(), header));
            long timeMillis = System.currentTimeMillis() - time;
            if (timeMillis > GameConst.COST_TIME) {
                LoggerUtils.performance.info("Choose_PlayTypeReq_50017,cost time:{},req:{}", timeMillis, req.toString());
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            long useTimes = System.currentTimeMillis() - time;
            if(useTimes > 500) {
                logger.error("ChoosePlayType。 耗时->{} 毫秒, reqNum->{}", useTimes, header.reqNum); //TODO:临时增加
            }
        }
    }
}
